<template>
  <div>
    <el-table
      :data="noticeList"
      :show-header="false"
      @row-click="openDetails"
      style="width: 100%;cursor:pointer">
      <el-table-column
        label="标题">
        <template slot-scope="scope">
          <i class="el-icon-caret-right" style="color: #409EFF"></i>
          <span style="color: red" v-if="scope.row.isTop === 0">{{scope.row.title}}</span>
          <span v-else>{{scope.row.title}}</span>

        </template>
      </el-table-column>
      <el-table-column
        prop="time"
        align="right"
        :formatter="dateFormat"
        label="时间">
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin: 10px"
      background
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import moment from "moment"
  import {noticeList,qian} from '../../axios/api'
  import qs from "qs"
    export default {
        name: "noticeList",
        data(){
            return{
                total:1000,
                noticeList:[],
                page:1
            }
        },
        mounted() {
            this.getMSG(1,10);
        },
        methods:{
            getMSG:async function(page,limit){
                let data = {
                    page:page,
                    limit:limit
                };
                let res = await noticeList(data);
                if (res.code === 0){
                    let notices = [];
                    res.page.list.forEach(function (data,i,a) {
                        notices.push({
                            id:data.id,
                            title:data.title,
                            url:data.url,
                            isTop:data.isTop,
                            time:data.createTime
                        })
                    });
                    this.noticeList = notices;
                    this.total = res.page.totalCount;
                }
            },
            dateFormat:function(row, column) {
                var date = row.time;
                if (date == undefined) {
                    return "";
                }
                return moment(parseInt(date)).format("YYYY-MM-DD");
            },
            handleCurrentChange(index){
              this.page = index;
              this.getMSG(index,10)
            },
            openDetails(row){
              this.setQian(row);
            },
            setQian:async function(row){
                let data = {"noticeId":row.id}
                let res = await qian(data);
                if (res.code === 0){
                    if (row.url === ""){
                        this.$router.push({ path:'/user/msg?id='+row.id})
                    }else{
                        window.open(row.url,'_blank');
                        history.go(0)
                    }
                }
            }
        }
    }
</script>

<style scoped>

</style>
